package diamond.day03;

class Number implements Runnable{
    private int number = 1;
    public void run() {
        while (true){
            synchronized(this){
                notify(); // 唤醒阻塞的线程
                if(number <= 100){
                    try {
                        Thread.sleep(100);// sleep()不会释放锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+":"+number);
                    number++;
                    try {
                        // 使得调用如下wait()方法的线程进入阻塞状态；wait（）会释放锁
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    break;
                }
            }
        }
    }
}

public class CommunicationTest {
    public static void main(String[] args) {
        Number number = new Number();
        Thread t1 = new Thread(number);
        Thread t2 = new Thread(number);
        t1.start();
        t2.start();
    }
}
